﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TextCompareInstruction:InstructionBase
    {
        #region props
        private string _ConnStr1;
        /// <summary></summary>
        public string ConnStr1
        {
            get
            {
                return this._ConnStr1;
            }
            set
            {
                this._ConnStr1 = value;
            }
        }

        private string _Schema1;
        /// <summary></summary>
        public string Schema1
        {
            get
            {
                return this._Schema1;
            }
            set
            {
                this._Schema1 = value;
            }
        }

        private string _Table1;
        /// <summary></summary>
        public string Table1
        {
            get
            {
                return this._Table1;
            }
            set
            {
                this._Table1 = value;
            }
        }

        private string _Sql1;
        /// <summary></summary>
        public string Sql1
        {
            get
            {
                return this._Sql1;
            }
            set
            {
                this._Sql1 = value;
            }
        }

        private string _KeyField1;
        /// <summary></summary>
        public string KeyField1
        {
            get
            {
                return this._KeyField1;
            }
            set
            {
                this._KeyField1 = value;
            }
        }

        private string _TextField1;
        /// <summary></summary>
        public string TextField1
        {
            get
            {
                return this._TextField1;
            }
            set
            {
                this._TextField1 = value;
            }
        }

        private string _ConnStr2;
        /// <summary></summary>
        public string ConnStr2
        {
            get
            {
                return this._ConnStr2;
            }
            set
            {
                this._ConnStr2 = value;
            }
        }

        private string _Schema2;
        /// <summary></summary>
        public string Schema2
        {
            get
            {
                return this._Schema2;
            }
            set
            {
                this._Schema2 = value;
            }
        }

        private string _Table2;
        /// <summary></summary>
        public string Table2
        {
            get
            {
                return this._Table2;
            }
            set
            {
                this._Table2 = value;
            }
        }

        private string _Sql2;
        /// <summary></summary>
        public string Sql2
        {
            get
            {
                return this._Sql2;
            }
            set
            {
                this._Sql2 = value;
            }
        }

        private string _KeyField2;
        /// <summary></summary>
        public string KeyField2
        {
            get
            {
                return this._KeyField2;
            }
            set
            {
                this._KeyField2 = value;
            }
        }

        private string _TextField2;
        /// <summary></summary>
        public string TextField2
        {
            get
            {
                return this._TextField2;
            }
            set
            {
                this._TextField2 = value;
            }
        }

        private string _OutputFilePath;
        /// <summary></summary>
        public string OutputFilePath
        {
            get
            {
                return this._OutputFilePath;
            }
            set
            {
                this._OutputFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public TextCompareInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._ConnStr1 = string.Empty;
            this._Schema1 = string.Empty;
            this._Table1 = string.Empty;
            this._Sql1 = string.Empty;
            this._KeyField1 = string.Empty;
            this._TextField1 = string.Empty;

            this._ConnStr2 = string.Empty;
            this._Schema2 = string.Empty;
            this._Table2 = string.Empty;
            this._Sql2 = string.Empty;
            this._KeyField2 = string.Empty;
            this._TextField2 = string.Empty;

            this._OutputFilePath = string.Empty;
        }

        /// <summary>
        /// constructor, all derived class must call default constructor to initialize
        /// default settings.
        /// </summary>
        public TextCompareInstruction(Guid componentID, Guid jobTicketID,
            string connStr1, string schema1, string tbl1, string sql1, string keyField1, string txtField1,
            string connStr2, string schema2, string tbl2, string sql2, string keyField2, string txtField2,
            string outputFilePath) 
            : base(componentID, jobTicketID)
        {
            this._ConnStr1 = connStr1;
            this._Schema1 = schema1;
            this._Table1 = tbl1;
            this._Sql1 = sql1;
            this._KeyField1 = keyField1;
            this._TextField1 = txtField1;

            this._ConnStr2 = connStr2;
            this._Schema2 = schema2;
            this._Table2 = tbl2;
            this._Sql2 = sql2;
            this._KeyField2 = keyField2;
            this._TextField2 = txtField2;

            this._OutputFilePath = outputFilePath;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr1",this.ConnStr1);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Schema1", this.Schema1);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Table1", this.Table1);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Sql1", this.Sql1);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField1", this.KeyField1);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TextField1", this.TextField1);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr2", this.ConnStr2);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Schema2", this.Schema2);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Table2", this.Table2);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Sql2", this.Sql2);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField2", this.KeyField2);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TextField2", this.TextField2);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputFilePath", this.OutputFilePath);
            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TextCompareInstruction instruction = new TextCompareInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.ConnStr1 = XmlDataUtil.GetAttributeValue(xNode, "ConnStr1", "");
                instruction.Schema1 = XmlDataUtil.GetAttributeValue(xNode, "Schema1", "");
                instruction.Table1 = XmlDataUtil.GetAttributeValue(xNode, "Table1", "");
                instruction.Sql1 = XmlDataUtil.GetAttributeValue(xNode, "Sql1", "");
                instruction.KeyField1 = XmlDataUtil.GetAttributeValue(xNode, "KeyField1", "");
                instruction.TextField1 = XmlDataUtil.GetAttributeValue(xNode, "TextField1", "");

                instruction.ConnStr2 = XmlDataUtil.GetAttributeValue(xNode, "ConnStr2", "");
                instruction.Schema2 = XmlDataUtil.GetAttributeValue(xNode, "Schema2", "");
                instruction.Table2 = XmlDataUtil.GetAttributeValue(xNode, "Table2", "");
                instruction.Sql2 = XmlDataUtil.GetAttributeValue(xNode, "Sql2", "");
                instruction.KeyField2 = XmlDataUtil.GetAttributeValue(xNode, "KeyField2", "");
                instruction.TextField2 = XmlDataUtil.GetAttributeValue(xNode, "TextField2", "");

                instruction.OutputFilePath = XmlDataUtil.GetAttributeValue(xNode, "OutputFilePath", "");
                
                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
